<template>
    <div class="image-comparison-container">
        <img-comparison-slider>
            <img slot="first" :src="beforeImage" :alt="beforeAlt" />
            <img slot="second" :src="afterImage" :alt="afterAlt" />
            <div slot="handle">
                <div class="handle-icon">
                    <div class="arrow left"></div>
                    <div class="arrow right"></div>
                </div>
            </div>
        </img-comparison-slider>
    </div>
</template>

<script setup>
import { defineProps, onMounted } from 'vue'
import 'img-comparison-slider'

const props = defineProps({
    beforeImage: {
        type: String,
        required: true
    },
    afterImage: {
        type: String,
        required: true
    },
    beforeAlt: {
        type: String,
        default: '处理前'
    },
    afterAlt: {
        type: String,
        default: '处理后'
    },
    width: {
        type: [String, Number],
        default: '100%'
    },
    height: {
        type: [String, Number],
        default: 'auto'
    }
})

onMounted(() => {
    // 确保组件完全加载后再初始化滑块位置
    const slider = document.querySelector('img-comparison-slider')
    if (slider) {
        slider.style.width = typeof props.width === 'number' ? `${props.width}px` : props.width
        slider.style.height = typeof props.height === 'number' ? `${props.height}px` : props.height
    }
})
</script>

<style lang="scss" scoped>
.image-comparison-container {
    width: 100%;
    position: relative;
    overflow: hidden;
    border-radius: 8px;
    //box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);

    :deep(img-comparison-slider) {
        --divider-width: 2px;
        --divider-color: #fff;
        --default-handle-width: 40px;
        --default-handle-color: #fff;

        img {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }

        // 自定义滑块样式
        .handle-icon {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            background: #fff;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
            display: flex;
            align-items: center;
            justify-content: center;
            position: relative;
            cursor: ew-resize;

            &::before {
                content: '';
                position: absolute;
                width: 2px;
                height: 24px;
                background: #666;
                border-radius: 1px;
            }

            .arrow {
                position: absolute;
                width: 0;
                height: 0;
                border-top: 6px solid transparent;
                border-bottom: 6px solid transparent;

                &.left {
                    border-right: 8px solid #666;
                    left: 8px;
                }

                &.right {
                    border-left: 8px solid #666;
                    right: 8px;
                }
            }
        }
    }

    // 添加加载动画
    .loading {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        color: #fff;
        background: rgba(0, 0, 0, 0.5);
        padding: 8px 16px;
        border-radius: 4px;
    }
}
</style>
